package com.bus;

import java.util.List;

import android.app.Dialog;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.LinearLayout;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class BusMapActivity extends MapActivity 
{       
        private LinearLayout linearLayout;
        private MapView mapView;
        private List<Overlay> mapOverlays;
        private BusMapOverlay itemizedOverlay; 
        private MapController mapController;
        private GeoPoint defaultMapCenter = new GeoPoint(40422900, -86896000);
        private int defaultZoomLevel = 12;
        
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.mapView = (MapView) findViewById(R.id.mapview);
        this.mapView.setBuiltInZoomControls(true);
        
        this.mapOverlays =  this.mapView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker); 
        this.itemizedOverlay = new BusMapOverlay(drawable, this);
        
        mapController = mapView.getController();
       
        mapController.setCenter(defaultMapCenter);
        mapController.setZoom(defaultZoomLevel);
        
        itemizedOverlay.updateOverlay();
        mapOverlays.add(itemizedOverlay);
        
    }
    
    @Override
    protected boolean isRouteDisplayed() 
    {
        return false;
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;
    }
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu)
    {
        if(mapView.isSatellite())
        {
                menu.findItem(R.id.satellite).setChecked(true);
        }
        else
        {
                menu.findItem(R.id.map).setChecked(true);
        }
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch(item.getItemId())
        {
        case R.id.routes:
                showDialog(1);
//              itemizedOverlay.updateOverlay();
//              mapOverlays.clear();
//              mapOverlays.add(itemizedOverlay);
//              mapView.invalidate();
                return true;
        case R.id.help:
                return true;
        case R.id.map:
                item.setChecked(true);
                mapView.setSatellite(false);
                return true;
        case R.id.satellite:
                item.setChecked(true);
                mapView.setSatellite(true);
                return true;
        case R.id.recenter:
            mapController.setCenter(defaultMapCenter);
            mapController.setZoom(defaultZoomLevel);
            return true;
        default:
                return super.onOptionsItemSelected(item);
        }
        
    }
    
    protected Dialog onCreateDialog(int i)
    {
        switch(i)
        {
                case 1:
                {
                        return itemizedOverlay.updateRoutesDialog();
                }
                default:
                {
                                return null;
                }
        }
    }
}
